<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'" label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="文件夹目录" prop="folder.id"
                :rules="[
                 ]">
          <el-tree-select
	          value-key="id"
	          :props="{
	              label: 'name',         // 显示名称
	              children: 'children'    // 子级字段名
	            }"
               placeholder="请选择文件夹目录"
              :data="projectDocumentFolderTreeData"
              v-model="inputForm.folder.id"
              :clearable="true"
              :accordion="true"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="文件名称" prop="documentName"
                :rules="[
                 ]">
          <el-input v-model="inputForm.documentName" placeholder="请填写文件名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="文件路径" prop="documentUrl"
                :rules="[
                 ]">
	            <file-upload v-model="inputForm.documentUrl" :drag="true" :limit="3" tip="最多上传3个图片,单个图片不要超过10M"></file-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="文件描述" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写文件描述"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>
  </v-dialog>
</template>

<script>
  import projectDocumentService from '@/api/document/projectDocumentService'
  import projectDocumentFolderService from '@/api/document/projectDocumentFolderService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        projectDocumentFolderTreeData: [],
        inputForm: {
          id: '',
          folder: {
            id: ''
          },
          documentName: '',
          documentUrl: '',
          remarks: '',
          fileName:'',
        }
      }
    },
    components: {
    },
    watch: {
    'inputForm.attachment': {
      handler(newVal) {
        if (newVal) {
          // 获取所有文件信息
          const files = newVal.split(',');
          const fileNames = [];
          const fileUrls = [];

          files.forEach(file => {
            if (file&&file.indexOf(',')) {
              const urlArr = file.split('&fileName=');
              if (urlArr.length >= 2) {
                const fileName = urlArr[urlArr.length - 1];
                const fileUrl = urlArr[0];
                fileNames.push(fileName);
                fileUrls.push(fileUrl);
              } else {
                return;
              }
            }
          });         
          if (fileNames.length) {
            this.inputForm.fileName = fileNames.join(',');
            // 保存纯文件地址（不带文件名）
            // this.inputForm.attachment = fileUrls.join(',');
          }
        }
      },
      immediate: true
    }
  },
    created () {
      projectDocumentFolderService.treeData().then((data) => {
          this.folderTreeData = data
      })
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建项目文件`
        } else if (method === 'edit') {
          this.title = '修改项目文件'
        } else if (method === 'view') {
          this.title = '查看项目文件'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          projectDocumentFolderService.treeData().then((data) => {
            this.projectDocumentFolderTreeData = data
          })
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            projectDocumentService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            projectDocumentService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>


